/**
 * 描述:测试文本宽度
 *  */
export const getTextWidth = (text: string, font = "12px Microsoft Yahei") => {
  const canvas = document.createElement("canvas");
  canvas.id = "__canvas__";
  document.body.append(canvas);
  const kanvas = document.querySelector("#__canvas__") as HTMLCanvasElement;
  const ctx = kanvas.getContext("2d") as CanvasRenderingContext2D;
  ctx.font = font;
  ctx.textBaseline = "middle";
  const info = ctx.measureText(text);
  canvas.remove();
  return info;
};

const createDom = (config: any, text: string) => {
  const div = document.createElement("div");
  div.hidden = true;
  div.innerHTML = text;
  div.style.display = "inline-block";
  div.style.fontFamily = config.fontFamily;
  div.style.lineHeight = "normal";
  div.style.whiteSpace = config.wrap;
  div.style.fontSize = `${config.fontSize}px`;
  document.body.append(div);
  return div;
};
export const getTextDomRect = (config: any) => {
  const div = createDom(config, config.text);
  const domRect = div.getBoundingClientRect();
  div.remove();
  return domRect;
};
export const getTextStyle = (config: any) => {
  const div = createDom(config, config.text);
  const style = getComputedStyle(div);
  div.remove();
  return style;
};
